package com.bw.zg2.view.fragment

import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.bw.zg2.Const
import com.bw.zg2.R
import com.bw.zg2.base.BaseFragment
import com.bw.zg2.bean.GoodsBeanItem
import com.bw.zg2.databinding.FragmentHomeBinding
import com.bw.zg2.view.activity.GoodsDetailActivity
import com.bw.zg2.view.activity.SearchActivity
import com.bw.zg2.view.adapter.HomeGoodsAdapter
import com.bw.zg2.viewmodel.HomeViewModel
import com.jcodecraeer.xrecyclerview.XRecyclerView.LoadingListener
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter.OnItemClickListener

/**
 * create an instance of this fragment.
 */
class HomeFragment : BaseFragment<HomeViewModel, FragmentHomeBinding>(R.layout.fragment_home) {
    private var id: Int = 0
    private var page: Int = 1
    private var size: Int = 10
    private lateinit var adapter: HomeGoodsAdapter
    private var goodsList = mutableListOf<GoodsBeanItem>()
    private var searchList = mutableListOf<GoodsBeanItem>()
    override fun initData() {
        //设置banner
        viewModel.bannerLiveData.observe(this) {
            if (it.data != null) {
                dataBinding.banner.setData(it.data, null)
                //
                dataBinding.banner.setmAdapter { _, _, view, position ->
                    activity?.let { ac ->
                        Glide.with(ac).load(it.data.get(position).imagePath).into(view as ImageView)
                    }
                }

                dataBinding.banner.setAutoPlayAble(true)
                dataBinding.banner.setAutoPlayTime(1500)
            }
        }

        //设置列表
        viewModel.homeListLiveData.observe(this) {
            //当page=1时，需要清空goodsList
            if (page == 1) {
                goodsList.clear()
            }
            goodsList.addAll(it.data)
            adapter.notifyDataSetChanged()
            //隐藏加载更多动画效果
            dataBinding.rv.reset()
        }
    }

    override fun initView() {
        //获取banner列表
        viewModel.getBanner()
        //获取首页列表
        viewModel.getHomeList(id, page, size)

        dataBinding.rv.layoutManager = GridLayoutManager(activity, 2)
        adapter = HomeGoodsAdapter(context, R.layout.item_goods_layout, goodsList)
        dataBinding.rv.adapter = adapter


        adapter.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(p0: View?, p1: RecyclerView.ViewHolder?, p2: Int) {
                val intent = Intent(activity, GoodsDetailActivity::class.java)
                intent.putExtra("goodsId", goodsList[p2 - 1].id)
//                intent.putExtra("bean", goodsList[p2 - 1)
                startActivity(intent)
//                ARouter.getInstance()
//                    .build(Const.PATH_LOGIN)
//                    .withInt("goodsId", goodsList[p2 - 1].id)
//                    .navigation()
            }

            override fun onItemLongClick(
                p0: View?,
                p1: RecyclerView.ViewHolder?,
                p2: Int
            ): Boolean {
                return false
            }

        })

        //处理刷新及加载更多
        dataBinding.rv.setLoadingListener(object : LoadingListener {
            override fun onRefresh() {
                //刷新，请求第一页数据
                page = 1
                viewModel.getHomeList(id, page, size)
            }

            override fun onLoadMore() {
                //加载更多，请求下一页数据
                page++
                viewModel.getHomeList(id, page, size)
            }
        })
        //获取搜索控件
//        dataBinding.searchView.getEtSearch()
        dataBinding.searchView.etSearch.addTextChangedListener(object : TextWatcher {

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun afterTextChanged(et: Editable?) {
                //根据输入内容，搜索商品列表
//                goodsList.clear()
//                page = 1
//                if (!TextUtils.isEmpty(et?.toString())) {
//                    viewModel.getHomeList(et?.toString()!!.toInt(), page, size)
//                } else {
//                    viewModel.getHomeList(0, page, size)
//                }

                if (!TextUtils.isEmpty(et?.toString())) {
                    goodsList.forEach {
                        if (it.goods_desc.contains(et!!.toString())) {
                            //把包含搜索关键字的数据添加到searchList
                            searchList.add(it)
                        }
                    }
                    //重写设置列表数据
                    dataBinding.rv.layoutManager = GridLayoutManager(activity, 2)
                    val adapter =
                        HomeGoodsAdapter(context, R.layout.item_goods_layout, searchList)
                    dataBinding.rv.adapter = adapter

                } else {
                    dataBinding.rv.layoutManager = GridLayoutManager(activity, 2)
                    adapter = HomeGoodsAdapter(context, R.layout.item_goods_layout, goodsList)
                    dataBinding.rv.adapter = adapter
                }

            }

        })


        dataBinding.searchView.imgSearch.setOnClickListener {
            startActivity(Intent(activity, SearchActivity::class.java))
        }

    }


}